//
//  EMConfirmViewController.m
//  ElectronicMall
//
//  Created by 史伟文 on 14/12/23.
//  Copyright (c) 2014年 cczu. All rights reserved.
//

#import "EMConfirmViewController.h"
#import "UINavigationController+EMNavi.h"
#import "EMOrderAddressCell.h"
#import "EMOrderSubmitViewController.h"
#import "EMTypeSegControl.h"

#import "EMAddressInfo.h"
#import "EMSettleRespBody.h"
#import "EMGoodInfo2.h"

#import "AppDelegate.h"
#import "MBProgressHUD.h"
#import "EMPostageReqBody.h"
#import "EMPostageRespBody.h"

#import "EMSubmitOrderReqBody.h"
#import "EMSubmitOrderRespBody.h"

#import "EMFetchShopReqBody.h"
#import "EMFetchShopRespBody.h"
#import "EMGetAddressReqBody.h"
#import "EMGetAddressRespBody.h"

#import "EMAddressMgrViewController.h"
#import "EMDotSegControl.h"
#import "EMPopView.h"
#import "UserInfo.h"
@interface EMConfirmViewController ()
{
    BOOL isOpenScroeCell;
    BOOL isOpenTicketCell;
    
    float total;
    UITableView *comfirmTable;
    
    AppDelegate *app;
    NSString *mailPrice;
    
    NSString *payMode;
    NSString *orderType;
    BOOL isNeedTicket;
    UITextField *comNameTF;
    
    NSArray *fetchShopArray;
    int storeId;
    UITextField *scoreTF;
    UILabel *totalLabelR;
    UILabel *scoreLabelR;
}
@end

@implementation EMConfirmViewController
- (void)hideBottomBar
{
    for(UIView *view in self.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]])
            view.hidden = YES;
    }
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self hideBottomBar];
    [self.navigationController setNavigationMode:EMNavigationModeDefault andSubMode:0];
    self.title = @"确认订单";
    
    fetchShopArray = [NSArray array];
    [self getUserAddress];
    [self getPostPrice];
    [self getFetchShopInfo];
    
    UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 30, 32);
    [button setImage:[UIImage imageNamed:@"button_导航_返回_白"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item =[[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.leftBarButtonItem =item;
    
    NSIndexSet *indexSet0 = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)];
    [comfirmTable reloadSections:indexSet0 withRowAnimation:UITableViewRowAnimationAutomatic];
    NSIndexSet *indexSet5 = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(5, 1)];
    [comfirmTable reloadSections:indexSet5 withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (void)backAction: (UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    isOpenScroeCell = NO;
    isOpenTicketCell = NO;
    
    comfirmTable = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    comfirmTable.delegate = self;
    comfirmTable.dataSource = self;
    comfirmTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    comfirmTable.backgroundColor = RGB(244, 244, 244);
    comfirmTable.bounces = NO;
    [self.view addSubview:comfirmTable];
    
    

}
- (void)tapTable:(UIGestureRecognizer *)recog
{
    [scoreTF resignFirstResponder];
}
- (void)getFetchShopInfo
{
    [MBProgressHUD  showHUDAddedTo:self.navigationController.view animated:YES];
    EMFetchShopReqBody *reqBody = [[EMFetchShopReqBody alloc] init];
    reqBody.JUDGEMETHOD = @"INVITE-SHOP";
    HessianFormDataRequest *request = [[HessianFormDataRequest alloc] init];
    request.postData = reqBody.keyValues;
    [request startRequestWithURL:baseURL andCompletionBlcok:^(NSDictionary *respInfo) {
        EMFetchShopRespBody *respBody = [EMFetchShopRespBody objectWithKeyValues:respInfo];
        [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
        NSLog(@"%@", respInfo);
        if ([respBody.ERRORCODE isEqualToString: @"0000"]) {
            fetchShopArray = respBody.SUCCESSFULINFO;
        } else {
            [app showTip:respInfo[@"ERRORDESTRIPTION"]];
        }
        
    } andFailedBlock:^{
        
        [app showTip:@"无法连接服务器、请稍后再试"];
    }];
    
}

- (void)getPostPrice
{
    EMSettleRespBody *resp = self.goodList;
    EMAddressInfo *defaultAddress;
    for (int i = 0; i < self.addressArray.count; i ++) {
        EMAddressInfo *addressInfo = [[EMAddressInfo alloc] initWithAddressInfo:self.addressArray[i]];
        if ([addressInfo.m_isDefault isEqualToString:@"1"]) {
            defaultAddress = addressInfo;
        }
    }
    EMPostageReqBody *reqBody = [[EMPostageReqBody alloc] init];
    reqBody.JUDGEMETHOD = @"CALCULATE-POSTAGE";
    reqBody.USERID = [UserInfo sharedUserInfo].m_userName;
    reqBody.SESSION = [UserInfo sharedUserInfo].m_session;
    reqBody.PRO = defaultAddress.m_provinceCode;
    reqBody.PRICE = resp.ALLPRICE;
    reqBody.WEIGHT = resp.ALLWEIGHT;
    HessianFormDataRequest *request = [[HessianFormDataRequest alloc] init];
    request.postData = reqBody.keyValues;
    [request startRequestWithURL:baseURL andCompletionBlcok:^(NSDictionary *respInfo) {
        EMPostageRespBody *respBody = [EMPostageRespBody objectWithKeyValues:respInfo];
        [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
        NSLog(@"%@", respInfo);
        if ([respBody.ERRORCODE isEqualToString: @"0000"]) {
            mailPrice = respBody.SUCCESSFULINFO;
        } else {
            [app showTip:respInfo[@"ERRORDESTRIPTION"]];
        }
        
    } andFailedBlock:^{
        
        [app showTip:@"无法连接服务器、请稍后再试"];
    }];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 7;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    if (section == 0) {
        return 1;
    } else if (section == 1) {
        EMSettleRespBody *resp = self.goodList;
        return resp.GOODINFO.count + 2;
    } else if (section == 2 || section == 5) {
        return 2;
    } else if (section == 4) {
        if (isOpenScroeCell) {
            return 2;
            isOpenScroeCell = NO;
        } else {
            return 1;
        }
    } else if (section == 3) {
        if (isOpenTicketCell) {
            return 2;
            isOpenTicketCell = NO;
        } else {
            return 1;
        }
    } else {
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSArray *indentifierArray = @[@"cellorder0", @"cellorder1", @"cellorder1", @"cellorder1", @"cellorder1", @"cellorder1", @"cellorder1"];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifierArray[indexPath.section]];
    
    while ([cell.subviews lastObject] != nil) {
        [(UIView *)[cell.subviews lastObject] removeFromSuperview];
    }
    EMSettleRespBody *resp = self.goodList;
    if (indexPath.section == 0) {
        
        EMAddressInfo *defaultAddress;
        for (int i = 0; i < self.addressArray.count; i ++) {
            EMAddressInfo *addressInfo = [[EMAddressInfo alloc] initWithAddressInfo:self.addressArray[i]];
            if ([addressInfo.m_isDefault isEqualToString:@"1"]) {
                defaultAddress = addressInfo;
            }
        }
        if (defaultAddress == nil) {
            UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];
            cell.textLabel.text = @"您未设置默认地址，点击设置";
            return cell;
        } else {
            EMOrderAddressCell *cell = [[EMOrderAddressCell alloc] initWithFrame:CGRectZero];
            cell.name.text = defaultAddress.m_name;
            cell.mobile.text = defaultAddress.m_mobile;
            cell.address.text = [NSString stringWithFormat:@"收获地址：%@%@%@%@",defaultAddress.m_province, defaultAddress.m_city, defaultAddress.m_area, defaultAddress.m_detail];
            return cell;
        }
        
    } else if (indexPath.section == 1) {
        cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:indentifierArray[indexPath.section]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UILabel *goodTitleLabel = [[UILabel alloc] init];
        UILabel *priceLabel = [[UILabel alloc] init];
        
        if (indexPath.row == 0) {
            UIView *tlineView = [[UIView alloc] init];
            tlineView.frame = CGRectMake(0, 0, W(320), 1);
            tlineView.backgroundColor = RGB(210, 230, 200);
            [cell addSubview:tlineView];
            goodTitleLabel.frame = CGRectMake(W(15), H(15), W(100), H(20));
            goodTitleLabel.text = @"商品清单";
            goodTitleLabel.textColor = [UIColor grayColor];
            goodTitleLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
            priceLabel.frame = CGRectMake(W(230), H(17.5), W(100), H(20));
            priceLabel.text = [NSString stringWithFormat:@"(共%@件商品)", resp.ALLCLASS];
            priceLabel.textColor = [UIColor grayColor];
            priceLabel.font = [UIFont fontWithName:HEITILIGHT size:14];
            tlineView.frame = CGRectMake(W(15), H(42), W(290), 2);
            tlineView.backgroundColor = RGB(210, 230, 200);
            [cell addSubview:tlineView];
        } else if (indexPath.row == resp.GOODINFO.count + 1) {
            UIView *tlineView = [[UIView alloc] init];
            tlineView.frame = CGRectMake(W(15), 0, W(290), 2);
            tlineView.backgroundColor = RGB(210, 230, 200);
            [cell addSubview:tlineView];
            goodTitleLabel.frame = CGRectMake(W(30), H(5), W(100), H(20));
            goodTitleLabel.text = [NSString stringWithFormat:@"共%@件商品", resp.ALLCLASS] ;
            goodTitleLabel.textColor = [UIColor grayColor];
            goodTitleLabel.font = [UIFont fontWithName:HEITIMEDIUM size:14];
            priceLabel.frame = CGRectMake(W(140), H(5), W(160), H(20));
            
            
            priceLabel.text = [NSString stringWithFormat:@"总价：%.2f元", [resp.ALLPRICE floatValue]] ;
            priceLabel.textColor = [UIColor darkGrayColor];
            priceLabel.textAlignment = NSTextAlignmentRight;
            priceLabel.font = [UIFont fontWithName:HEITILIGHT size:14];
            UIView *blineView = [[UIView alloc] init];
            blineView.frame = CGRectMake(0, H(29), W(320), 1);
            blineView.backgroundColor = RGB(210, 230, 200);
            [cell addSubview:blineView];
        } else {
            EMGoodInfo2 *info = [[EMGoodInfo2 alloc] initWithGoodInfo:resp.GOODINFO[indexPath.row-1]];
            goodTitleLabel.frame = CGRectMake(W(30), H(2), W(200), H(20));
            goodTitleLabel.text = info.m_name;
            goodTitleLabel.textColor = [UIColor grayColor];
            goodTitleLabel.font = [UIFont fontWithName:HEITIMEDIUM size:12];
            priceLabel.frame = CGRectMake(W(200), H(5), W(100), H(20));
            priceLabel.text = [NSString stringWithFormat:@"%@x%@=¥%.1f", info.m_price, info.m_count, [info.m_price floatValue] * [info.m_count intValue]];
            priceLabel.textColor = [UIColor grayColor];
            priceLabel.textAlignment = NSTextAlignmentRight;
            priceLabel.font = [UIFont fontWithName:HEITILIGHT size:12];
            UIView *tlineView = [[UIView alloc] init];
            tlineView.frame = CGRectMake(W(30), H(24), W(275), 1);
            tlineView.backgroundColor = RGB(210, 230, 200);
            [cell addSubview:tlineView];
        }
        [cell addSubview:goodTitleLabel];
        [cell addSubview:priceLabel];
        UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTable:)];
        [cell addGestureRecognizer:tapGR];
        return cell;
    } else if (indexPath.section == 2) {
        cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:indentifierArray[indexPath.section]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row == 0) {
            UIView *tlineView = [[UIView alloc] init];
            tlineView.frame = CGRectMake(0, 0, W(320), 1);
            tlineView.backgroundColor = RGB(210, 230, 200);
            [cell addSubview:tlineView];
            UILabel *typeTitleLabel = [[UILabel alloc] init];
            typeTitleLabel.frame = CGRectMake(15, 14, 100, 16);
            typeTitleLabel.text = @"订单类型";
            typeTitleLabel.textColor = [UIColor grayColor];
            typeTitleLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
            [cell addSubview:typeTitleLabel];
            EMTypeSegControl *seg = [[EMTypeSegControl alloc] initWithFrame:CGRectMake(100, 8, 168, 30)];
            [seg setTitleArray:@[@"配送订单", @"自取订单"]];
            [seg setActionBlock:^(NSUInteger type) {
                if (type == 0) {
                    orderType = @"2";
                } else {
//                    EMPopView *popView = [[EMPopView alloc] initWithTableView:@[@"111", @"222"]];
//                    [popView show];
                    
                    
                    EMAttention *attention = [[EMAttention alloc] initWithTitle:@"提示" contentText:@"[1865店] 正学路1号1865园区E14栋东2楼\n[新街口店] 新街口地铁站负一层东方商城16号出口" leftButtonTitle:@"新街口店" rightButtonTitle:@"1958店"];
                    attention.leftBlock = ^{storeId = 1;};
                    attention.rightBlock = ^{storeId = 2;};
                    [attention show];
                    orderType = @"1";
                }
            }];
            [cell addSubview:seg];
        } else {
            UILabel *typeTitleLabel = [[UILabel alloc] init];
            typeTitleLabel.frame = CGRectMake(15, 14, 100, 16);
            typeTitleLabel.text = @"支付方式";
            typeTitleLabel.textColor = [UIColor grayColor];
            typeTitleLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
            [cell addSubview:typeTitleLabel];
            EMTypeSegControl *seg = [[EMTypeSegControl alloc] initWithFrame:CGRectMake(100, 8, 168, 30)];
            [seg setTitleArray:@[@"货到付款", @"在线支付"]];
            [seg setActionBlock:^(NSUInteger mode) {
                if (mode == 0) {
                    payMode = @"1";
                } else {
                    payMode = @"2";
                }
            }];
            [cell addSubview:seg];
            UIView *blineView = [[UIView alloc] initWithFrame:CGRectMake(0, 43, W(320), 1)];
            blineView.backgroundColor = RGB(210, 230, 200);
            [cell addSubview:blineView];
        }
        UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTable:)];
        [cell addGestureRecognizer:tapGR];

        
        return cell;
    } else if (indexPath.section == 3) {
        cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:indentifierArray[indexPath.section]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row == 0) {
            UIView *tlineView = [[UIView alloc] init];
            tlineView.frame = CGRectMake(0, 0, W(320), 1);
            tlineView.backgroundColor = RGB(210, 230, 200);
            [cell addSubview:tlineView];
            UILabel *titleLabel = [[UILabel alloc] init];
            titleLabel.frame = CGRectMake(15, 14, 32, 16);
            titleLabel.text = @"发票";
            titleLabel.textColor = [UIColor grayColor];
            titleLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
            [cell addSubview:titleLabel];
            UIImageView *arrowImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_个人中心_前进"]];
            arrowImage.frame = CGRectMake(W(280), 5, 30, 33);
            if (isOpenTicketCell) {
                arrowImage.transform = CGAffineTransformMakeRotation(M_PI / 2);
            } else {
                arrowImage.transform = CGAffineTransformMakeRotation(0);
            }
            [cell addSubview:arrowImage];
            UIView *blineView = [[UIView alloc] init];
            blineView.frame = CGRectMake(0, 43, W(320), 1);
            blineView.backgroundColor = RGB(210, 230, 200);
            [cell addSubview:blineView];
            return cell;
        } else {
            
            // segmentControl
            EMDotSegControl *seg = [[EMDotSegControl alloc] initWithFrame:CGRectMake(30, 8, 100, 30)];
            [seg setTitleArray:@[@"不需要", @"需要"]];
            
            UIView *invoiceView = [[UIView alloc] initWithFrame:CGRectMake(150, 10, 150, 24)];
            invoiceView.hidden = YES;
            comNameTF = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 150, 24)];
            comNameTF.layer.cornerRadius = 3;
            comNameTF.layer.borderColor = [UIColor lightGrayColor].CGColor;
            comNameTF.layer.borderWidth = 1;
            comNameTF.placeholder = @"发票抬头";
            comNameTF.textColor = [UIColor darkGrayColor];
            comNameTF.font = [UIFont fontWithName:HEITILIGHT size:12];
            [invoiceView addSubview:comNameTF];
            [cell addSubview:invoiceView];
            
            [seg setActionBlock:^(NSUInteger index) {
                if (index == 0) {
                    invoiceView.hidden = YES;
                    isNeedTicket = NO;
                } else {
                    invoiceView.hidden = NO;
                    isNeedTicket = YES;
                }
            }];
            
            [cell addSubview:seg];
            
            UIView *blineView = [[UIView alloc] init];
            blineView.frame = CGRectMake(0, 43, W(320), 1);
            blineView.backgroundColor = RGB(210, 230, 200);
            [cell addSubview:blineView];
            return cell;
        }
        
    } else if (indexPath.section == 4) {
        cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:indentifierArray[indexPath.section]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UIImageView *iconImage;
        if (iconImage == nil) {
            iconImage = [[UIImageView alloc] init];
        }
        if (indexPath.row == 0) {
            UIView *tlineView = [[UIView alloc] init];
            tlineView.frame = CGRectMake(0, 0, W(320), 1);
            tlineView.backgroundColor = RGB(210, 230, 200);
            [cell addSubview:tlineView];
            iconImage.image = [UIImage imageNamed:@"icon_积分"];
            iconImage.frame = CGRectMake(15, 9, 36, 25);
            [cell addSubview:iconImage];
            UILabel *titleLabel = [[UILabel alloc] init];
            titleLabel.frame = CGRectMake(55, 14, 32, 16);
            titleLabel.text = @"积分";
            titleLabel.textColor = [UIColor grayColor];
            titleLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
            [cell addSubview:titleLabel];
            UILabel *scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(95, 15, 155, 14)];
            scoreLabel.font = [UIFont fontWithName:HEITILIGHT size:14];
            scoreLabel.text = [NSString stringWithFormat:@"%@积分可用", self.score];
            scoreLabel.textColor = RGB(112, 179, 81);
            [cell addSubview:scoreLabel];
            UIImageView *arrowImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_个人中心_前进"]];
            arrowImage.frame = CGRectMake(W(280), 5, 30, 33);
            if (isOpenScroeCell) {
                arrowImage.transform = CGAffineTransformMakeRotation(M_PI / 2);
            } else {
                arrowImage.transform = CGAffineTransformMakeRotation(0);
            }
            [cell addSubview:arrowImage];
            UIView *blineView = [[UIView alloc] init];
            blineView.frame = CGRectMake(0, 43, W(320), 1);
            blineView.backgroundColor = RGB(210, 230, 200);
            [cell addSubview:blineView];
            return cell;
        } else {
            
            UILabel *useScore = [[UILabel alloc] init];
            useScore.frame = CGRectMake(25, 14, 80, 16);
            useScore.text = @"使用积分：";
            useScore.textColor = [UIColor grayColor];
            useScore.font = [UIFont fontWithName:HEITIMEDIUM size:16];
            [cell addSubview:useScore];
            
            scoreTF = [[UITextField alloc] init];
            scoreTF.frame = CGRectMake(105, 10, 60, 24);
            scoreTF.text = self.score;
            scoreTF.textColor = [UIColor grayColor];
            scoreTF.textAlignment = NSTextAlignmentCenter;
            scoreTF.font = [UIFont fontWithName:HEITIMEDIUM size:14];
            scoreTF.layer.borderColor = [UIColor grayColor].CGColor;
            scoreTF.layer.borderWidth = 1;
            scoreTF.layer.cornerRadius = 5;
            scoreTF.delegate = self;
            scoreTF.keyboardType = UIKeyboardTypeNumberPad;
            [cell addSubview:scoreTF];
            
            UILabel *scoreToCash = [[UILabel alloc] init];
            scoreToCash.frame = CGRectMake(W(170), 14, 130, 16);
            scoreToCash.text = [NSString stringWithFormat:@"可抵现金:%.2f元", [scoreTF.text floatValue] / 100.0f];
            scoreToCash.textColor = RGB(112, 179, 81);
            scoreToCash.font = [UIFont fontWithName:HEITILIGHT size:14];
            [cell addSubview:scoreToCash];
            UIView *blineView = [[UIView alloc] init];
            blineView.frame = CGRectMake(0, 43, W(320), 1);
            blineView.backgroundColor = RGB(210, 230, 200);
            [cell addSubview:blineView];
            return cell;
        }
        
    } else if (indexPath.section == 5) {
        cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:indentifierArray[indexPath.section]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row == 0) {
            UIView *tlineView = [[UIView alloc] init];
            tlineView.frame = CGRectMake(0, 0, 320, 1);
            tlineView.backgroundColor = RGB(210, 230, 200);
            [cell addSubview:tlineView];
            UILabel *moneyLabel = [[UILabel alloc] init];
            moneyLabel.frame = CGRectMake(15, 12, 64, 16);
            moneyLabel.text = @"商品金额";
            moneyLabel.textColor = [UIColor grayColor];
            moneyLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
            [cell addSubview:moneyLabel];
            UILabel *moneyLabelR = [[UILabel alloc] init];
            moneyLabelR.frame = CGRectMake(W(240), 12, 64, 16);
            moneyLabelR.text = [NSString stringWithFormat:@"%.2f", [resp.ALLPRICE floatValue]];
            moneyLabelR.textColor = [UIColor redColor];
            moneyLabelR.textAlignment = NSTextAlignmentRight;
            moneyLabelR.font = [UIFont fontWithName:HEITIMEDIUM size:16];
            [cell addSubview:moneyLabelR];
            UILabel *transLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 33, 28, 20)];
            transLabel.text = @"运费";
            transLabel.textColor = [UIColor grayColor];
            transLabel.font = [UIFont fontWithName:HEITIMEDIUM size:14];
            [cell addSubview:transLabel];
            UILabel *transLabelR = [[UILabel alloc] initWithFrame:CGRectMake(W(240), 35, 64, 20)];
            transLabelR.text = mailPrice;
            transLabelR.textColor = [UIColor redColor];
            transLabelR.textAlignment = NSTextAlignmentRight;
            transLabelR.font = [UIFont fontWithName:HEITIMEDIUM size:14];
            [cell addSubview:transLabelR];
//            UILabel *scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 53, 56, 20)];
//            scoreLabel.text = @"积分扣减";
//            scoreLabel.textColor = [UIColor grayColor];
//            scoreLabel.font = [UIFont fontWithName:HEITIMEDIUM size:14];
//            [cell addSubview:scoreLabel];
//            scoreLabelR = [[UILabel alloc] initWithFrame:CGRectMake(240, 55, 64, 20)];
//            scoreLabelR.text = [NSString stringWithFormat:@"%.2f", [scoreTF.text floatValue] / 100];
//            scoreLabelR.textColor = [UIColor redColor];
//            scoreLabelR.textAlignment = NSTextAlignmentRight;
//            scoreLabelR.font = [UIFont fontWithName:HEITIMEDIUM size:14];
//            [cell addSubview:scoreLabelR];
           tlineView.frame = CGRectMake(15, 58, W(290), 2);
            tlineView.backgroundColor = RGB(210, 230, 200);
            [cell addSubview:tlineView];
        } else {
            cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:indentifierArray[indexPath.section]];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            UILabel *totalLabel = [[UILabel alloc] init];
            totalLabel.frame = CGRectMake(15, 12, 64, 16);
            totalLabel.text = @"应付总额";
            totalLabel.textColor = [UIColor grayColor];
            totalLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
            [cell addSubview:totalLabel];
            totalLabelR = [[UILabel alloc] init];
            totalLabelR.frame = CGRectMake(W(240), 13, 64, 16);
            total = [resp.ALLPRICE floatValue] + [mailPrice floatValue];
            totalLabelR.text = [NSString stringWithFormat:@"%.2f", total];
            totalLabelR.textColor = [UIColor redColor];
            totalLabelR.textAlignment = NSTextAlignmentRight;
            totalLabelR.font = [UIFont fontWithName:HEITIMEDIUM size:16];
            [cell addSubview:totalLabelR];
            UIView *tlineView = [[UIView alloc] init];
            tlineView.frame = CGRectMake(0, 39, W(320), 1);
            tlineView.backgroundColor = RGB(210, 230, 200);
            [cell addSubview:tlineView];
            
        }
        return cell;
    } else {
        cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:indentifierArray[indexPath.section]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = RGB(236, 236, 236);
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(10, 5, W(300), 50);
        button.layer.backgroundColor = [UIColor redColor].CGColor;
        button.layer.cornerRadius = 10;
        [button setTitle:@"提交订单" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(orderSubmit:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:button];
        
        return cell;
    }

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EMSettleRespBody *resp = self.goodList;
    if (indexPath.section == 0) {
        return 80;
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            return H(44);
        } else if (indexPath.row == resp.GOODINFO.count + 1){
            return H(30);
        } else {
            return H(24);
        }
    } else if (indexPath.section == 2 || indexPath.section == 3 || indexPath.section == 4) {
        return 44;
    } else if (indexPath.section == 5) {
        if (indexPath.row == 0) {
            return 60;
        } else {
            return 40;
        }
    } else{
        return 60;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([textField.text intValue] > [self.score intValue]) {
        [app showTip:@"积分不够"];
        string = self.score;
    }
    return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    total = total - [textField.text floatValue] / 100;
//    scoreLabelR.text = [NSString stringWithFormat:@"%.2f", [scoreTF.text floatValue] / 100];
//    totalLabelR.text = [NSString stringWithFormat:@"%.2f", total];
//    [comfirmTable reloadData];
    return YES;
}
- (void)orderSubmit: (UIButton *)sender
{
    EMSubmitOrderReqBody *reqBody = [[EMSubmitOrderReqBody alloc] init];
    reqBody.JUDGEMETHOD = @"SUBMIT-ORDER";
    reqBody.USERID = [UserInfo sharedUserInfo].m_userName;
    reqBody.SESSION = [UserInfo sharedUserInfo].m_session;

    if ([orderType isEqualToString:@"1"] || [orderType isEqualToString:@"2"]) {
        reqBody.GETWAY = orderType;
    } else {
        [app showTip:@"请选择订单类型"];
        [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
        return;
    }
    if ([payMode isEqualToString:@"1"] || [payMode isEqualToString:@"2"]) {
        reqBody.PAYTYPE = payMode;
    } else {
        [app showTip:@"请选择支付方式"];
        [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
        return;
    }
    if ([scoreTF.text intValue] > [self.score intValue]) {
        [app showTip:@"积分不够"];
        return;
    }
    NSString *string = [NSString stringWithFormat:@"您使用积分%@分，扣减%.2f元，实际支付金额%.2f元", scoreTF ? scoreTF.text : @"0", [scoreTF.text floatValue] / 100, total - [scoreTF.text floatValue] / 100];
    EMAttention *act = [[EMAttention alloc] initWithTitle:@"确认提交订单" contentText:string leftButtonTitle:nil rightButtonTitle:@"确定"];
    act.rightBlock = ^{
        NSMutableArray *goodArray = [NSMutableArray array];
        for (int i = 0; i < self.goodList.GOODINFO.count; i ++) {
            EMGoodInfo2 *goodInfo = [[EMGoodInfo2 alloc] initWithGoodInfo:self.goodList.GOODINFO[i]];
            [goodArray addObject:@{@"id":goodInfo.m_id, @"no":goodInfo.m_count}];
        }
        EMSettleRespBody *resp = self.goodList;
        [MBProgressHUD  showHUDAddedTo:self.navigationController.view animated:YES];
        if ([reqBody.GETWAY isEqualToString:@"2"]) {
            EMAddressInfo *defaultAddress;
            for (int i = 0; i < self.addressArray.count; i ++) {
                EMAddressInfo *addressInfo = [[EMAddressInfo alloc] initWithAddressInfo:self.addressArray[i]];
                if ([addressInfo.m_isDefault isEqualToString:@"1"]) {
                    defaultAddress = addressInfo;
                }
            }
            reqBody.ADDRESSID = defaultAddress.m_id;
            reqBody.POSTAGE = mailPrice;
            reqBody.INVITESHOP = @"";
        } else {
            reqBody.ADDRESSID = @"";
            reqBody.POSTAGE = @"";
            reqBody.INVITESHOP = [NSString stringWithFormat:@"%d", storeId];//自取店铺的id
        }
        reqBody.GOODSPRICE = [NSString stringWithFormat:@"%.2f", [resp.ALLPRICE floatValue]];
        total = [resp.ALLPRICE floatValue] + [mailPrice floatValue];
        reqBody.REALPRICE = [NSString stringWithFormat:@"%.2f", total];
        reqBody.SCORE = scoreTF == nil ? @"0" : scoreTF.text;
        if (isNeedTicket) {
            if ([comNameTF.text isEqualToString:@""]) {
                [app showTip:@"请填写发票抬头"];
                [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
                return;
            } else {
                reqBody.INVOICE = comNameTF.text;
            }
        } else {
            reqBody.INVOICE = @"";
        }
        
        reqBody.BUYGOODSINFO = [goodArray mutableCopy];
        HessianFormDataRequest *request = [[HessianFormDataRequest alloc] init];
        request.postData = reqBody.keyValues;
        [request startRequestWithURL:baseURL andCompletionBlcok:^(NSDictionary *respInfo) {
            EMSubmitOrderRespBody *respBody = [EMSubmitOrderRespBody objectWithKeyValues:respInfo];
            [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
            NSLog(@"%@", respInfo);
            if ([respBody.ERRORCODE isEqualToString: @"0000"]) {
                
                EMOrderSubmitViewController *controller = [[EMOrderSubmitViewController alloc] init];
                controller.orderId = respBody.ORDERNO;
                controller.payPrice = respBody.REALPRICE;
                controller.payType = respBody.PAYTYPE;
                controller.goodArray = resp.GOODINFO;
                [self.navigationController pushViewController:controller animated:YES];
                
            } else {
                [app showTip:respInfo[@"ERRORDESTRIPTION"]];
            }
            
        } andFailedBlock:^{
            
            [app showTip:@"无法连接服务器、请稍后再试"];
        }];

    };
    [act show];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        EMAddressMgrViewController *controller = [[EMAddressMgrViewController alloc] init];
        [self.navigationController pushViewController:controller animated:YES];
    } else if (indexPath.section == 3) {
        if (indexPath.row == 0) {
            isOpenTicketCell = !isOpenTicketCell;
            NSLog(@"%@", tableView.subviews);
            for (UIView *view in tableView.subviews) {
                if ([view isKindOfClass:[UIImageView class]]) {
                    [view removeFromSuperview];
                }
                
            }
            NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:3];
            [comfirmTable reloadSections:indexSet withRowAnimation:UITableViewRowAnimationNone];
        }
        
    } else if (indexPath.section == 4) {
        if (indexPath.row == 0) {
            isOpenScroeCell = !isOpenScroeCell;
            NSLog(@"%@", tableView.subviews);
            for (UIView *view in tableView.subviews) {
                if ([view isKindOfClass:[UIImageView class]]) {
                    [view removeFromSuperview];
                }
                
            }
            NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:4];
            [comfirmTable reloadSections:indexSet withRowAnimation:UITableViewRowAnimationNone];
        }
        
    }
}
- (void)getUserAddress
{
    [MBProgressHUD  showHUDAddedTo:self.navigationController.view animated:YES];
    EMGetAddressReqBody *reqBody = [[EMGetAddressReqBody alloc] init];
    reqBody.JUDGEMETHOD = @"GET-USER-ADDRESS";
    reqBody.USERID = [UserInfo sharedUserInfo].m_userName;
    reqBody.SESSION = [UserInfo sharedUserInfo].m_session;
    HessianFormDataRequest *request = [[HessianFormDataRequest alloc] init];
    request.postData = reqBody.keyValues;
    [request startRequestWithURL:baseURL andCompletionBlcok:^(NSDictionary *respInfo) {
        EMGetAddressRespBody *respBody = [EMGetAddressRespBody objectWithKeyValues:respInfo];
        [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
        NSLog(@"%@", respInfo);
        if ([respBody.ERRORCODE isEqualToString: @"0000"]) {
            self.addressArray = respBody.SUCCESSFULINFO;
        } else {
            [app showTip:respInfo[@"ERRORDESTRIPTION"]];
        }
        
    } andFailedBlock:^{
        
        [app showTip:@"无法连接服务器、请稍后再试"];
    }];
    
}

@end
